package seabattle.movements;

import seabattle.io.Command;
import seabattle.io.Reader;
import seabattle.ship.Coordinates;

public class PlayerMovement implements Movement {

	public static final String INPUT_MSG = "/ q - quit / shoot - a0 or A0 / >";
	public static final int MAX_LOOP_COUNT = 10000;
	private Reader reader;

	public PlayerMovement(Reader reader) {
		this.reader = reader;
	}

	public Coordinates getShot() {
		int count = 0;
		do {
			count++;
			Command command = new Command(reader.read(INPUT_MSG));
			if (command.isQuit()) {
				return null;
			}
			int x, y;
			try {
				y = command.getY();
				if (y < 0) {
					System.out.println(">entered an incorrect parameter");
					continue;
				}
			} catch (RuntimeException e) {
				System.out.println(">entered NULL parameter");
				continue;
			}
			try {
				x = command.getX();
			} catch (RuntimeException e) {
				System.out.println(">entered an incorrect parameter");
				continue;
			}
			if ((x > -1) && (y > -1)) {
				return new Coordinates(x, y);
			}
		} while (count < MAX_LOOP_COUNT);
		return null;
	}
}
